package com.purengage.ipilih;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.markupartist.android.widget.ActionBar;
import com.markupartist.android.widget.ActionBar.IntentAction;
import com.purengage.ipilih.lib.DBClass;
import com.purengage.ipilih.lib.FileHelper;
import com.purengage.ipilih.lib.FormatDate;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class Profile extends Activity {
	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_profile);

		final ActionBar actionBar = (ActionBar) findViewById(R.id.actionbar);
		actionBar.setTitle("Profile");
		actionBar.setHomeAction(new IntentAction(this, Survey2
				.createIntent(this), R.drawable.ic_action_back));

		DBClass db = new DBClass(this);

		TextView tvname = (TextView) findViewById(R.id.tvnama);
		tvname.setText(db.getValue("name"));

		TextView tvmobile = (TextView) findViewById(R.id.tvf1);
		tvmobile.setText(db.getValue("mobile"));

		TextView tvbirth = (TextView) findViewById(R.id.tvf2);
		tvbirth.setText(new FormatDate().convert(db.getValue("date_birth")));

		TextView tvprov = (TextView) findViewById(R.id.tvprov);
		TextView tvkab = (TextView) findViewById(R.id.tvkab);
		TextView tvkec = (TextView) findViewById(R.id.tvkec);
		TextView tvdes = (TextView) findViewById(R.id.tvdes);

		FileHelper f = new FileHelper(getResources().getString(
				R.string.client_name));
		String loc = f.read("locations");

		try {
			JSONObject jobj = new JSONObject(loc);
			JSONObject json = jobj.getJSONObject("data");

			tvprov.setText(json.getJSONObject("provinces").getJSONArray("data")
					.getJSONObject(0).getString("name"));

			tvkab.setText(json.getJSONObject("kabupatens").getJSONArray("data")
					.getJSONObject(0).getString("name"));

			tvkec.setText(json.getJSONObject("kecamatans").getJSONArray("data")
					.getJSONObject(0).getString("name"));

			JSONArray desa = json.getJSONObject("desas").getJSONArray("data");
			String desas = "";
			for (int i = 0; i < json.length(); i++) {
				JSONObject job = desa.getJSONObject(i);
				String pre = (i>0)?", ":"";
				desas += pre + job.getString("name");

			}
			
			tvdes.setText(desas);

		} catch (JSONException e) {
			e.printStackTrace();
		}

		// loadUI();
	}

	/*
	 * public void loadUI() { ViewGroup vg = (ViewGroup)
	 * findViewById(R.id.listpointdanrewards); LayoutInflater vi =
	 * (LayoutInflater) getApplicationContext()
	 * .getSystemService(Context.LAYOUT_INFLATER_SERVICE); for (int i = 0; i <
	 * 3; i++) { View v = vi.inflate(R.layout.row_pointandreward, null);
	 * vg.addView(v); }
	 * 
	 * ViewGroup ve = (ViewGroup) findViewById(R.id.liststatus); LayoutInflater
	 * vr = (LayoutInflater) getApplicationContext()
	 * .getSystemService(Context.LAYOUT_INFLATER_SERVICE); for (int i = 0; i <
	 * 3; i++) { View v = vr.inflate(R.layout.row_pointandreward, null);
	 * ve.addView(v); } }
	 */
}
